package com.leyou.server;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.mapper.SpecGroupMapper;
import com.leyou.mapper.SpecParamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecService {
    @Autowired(required = false)
    private SpecGroupMapper specGroupMapper;
    @Autowired(required = false)
    private SpecParamMapper specParamMapper;

    public List<SpecGroup> findSpecGroup(Long id) {
       //分装条件
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(id);
        QueryWrapper<SpecGroup> query = Wrappers.query(specGroup);
        List<SpecGroup> specGroups = specGroupMapper.selectList(query);
        return specGroups;
    }

    public List<SpecParam> findSpecParam(Long gid, Long cid, Boolean searching) {
        //1.封装条件
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);

        QueryWrapper<SpecParam> query = Wrappers.query(specParam);

        List<SpecParam> specParams = specParamMapper.selectList(query);
        return  specParams;
    }

    public List<SpecGroupDTO> findSpecGroupDTOByCid(Long id) {
          //根据id查询组
        List<SpecGroup> specGroups = findSpecGroup(id);
        List<SpecGroupDTO> specGroupDTOS = BeanHelper.copyWithCollection(specGroups, SpecGroupDTO.class);
        //遍历得到每一个组
        specGroupDTOS.forEach(specGroupDTO -> {
            List<SpecParam> specParam = findSpecParam(specGroupDTO.getId(), null, null);
            specGroupDTO.setParams(specParam);
        });
        return specGroupDTOS;
    }
}
